//
//  DYnamicUIView.swift
//  DynamicStateLJZ
//
//  Created by kys-11 on 2019/4/4.
//  Copyright © 2019年 xl123. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol LonginDelegate: NSObjectProtocol{
    
    func JumpPictureDetailsView(vc:ImgCollectionViewController)
    
    func update()
}//代理定义
class DYnamicUIView: UIView,UITableViewDelegate,UITableViewDataSource,clickLonginDelegate {
   
    func JumpPictureDetailsCell(vc:ImgCollectionViewController) {
        self.loc.JumpPictureDetailsView(vc: vc)
    }
    let header = MJRefreshNormalHeader()//顶部刷新
    let footer = MJRefreshAutoNormalFooter()    // 底部刷新
    var timer :Timer!
    var topImage: UIImageView!
    var DYtableview: UITableView!
    var userimage: UIImageView!
    var userlable: UILabel!
    
    var contenimage:[[String]] = []//内容图片
    var headimage:[String] = []//用户头像
    var contenString:[String] = []//说说内容
    var screenname:[String] = []//用户名
    var time:[String] = [] //d发表时间
    var loc:LonginDelegate!
    
    override init(frame: CGRect) {
        super.init(frame:frame)
     
     
        self.backgroundColor = UIColor.gray
        creatUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //加载数据
    func update(conten:String,timedata:String,contentimage:[String]){
        
        contenString.append(conten)//内容
//        screenname.append(username)//用户名
        time.append(timedata)//时间
        contenimage.append(contentimage)
//        headimage.append(heaimage)//头像

        DYtableview.reloadData()//重新加载数据
    }
    
    //设置UI界面
    func creatUI(){
        
//        topImage = UIImageView()
//        topImage.image = UIImage(named: "top2image.jpeg")
//        topImage.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
        
        
//        userimage = UIImageView()
//        userimage.image = UIImage(named: "xia1.gif")
//        topImage.addSubview(userimage)
//
//
//        userimage.snp.makeConstraints { (make) in
//            make.top.equalTo(topImage.snp.bottom).offset(-35)
//         make.right.equalTo(-8)
//            make.width.height.equalTo(60)
//        }
//        userimage.layer.masksToBounds = true
//        userimage.layer.cornerRadius = 30

//        userlable = UILabel()
//        userlable.text = "山山而川"
//        userlable.textColor = UIColor.white
//        topImage.addSubview(userlable)
//
//        userlable.snp.makeConstraints { (make) in
//            make.top.equalTo(userimage.snp.bottom).offset(12)
//            make.left.equalTo(userimage.snp.left)
//            make.height.equalTo(20)
//            make.width.equalTo(80)
//        }
      
        DYtableview = UITableView(frame: CGRect(x: 0, y: 0, width: Globalwidth, height: Globalheight), style: .plain)
        DYtableview.delegate = self
        DYtableview.dataSource = self
        
        DYtableview.backgroundColor = UIColor(displayP3Red: 207/255, green: 207/255, blue: 207/255, alpha: 1)
//  self.DYtableview.tableHeaderView = topImage
        DYtableview.showsHorizontalScrollIndicator = false
        DYtableview.showsVerticalScrollIndicator = false
        self.addSubview(DYtableview)
        DYtableview.estimatedRowHeight = 44.0
        DYtableview.rowHeight = UITableView.automaticDimension
        
        // 顶部刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        header.setTitle("你拉我干什么，你个臭流氓。。。", for: .pulling)
        header.setTitle("走开走开啦", for: .refreshing)
        self.DYtableview.mj_header = header
        //底部刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        footer.setTitle("刷新加载更多", for: .idle)
        footer.setTitle("正在刷新", for: .refreshing)
//        footer.setTitle("暂无更多数据", for: .noMoreData)
//        footer.setTitle("刷新完成", for: .pulling)
        self.DYtableview.mj_footer = footer
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    //返回分区个数
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contenString.count
    }
    
    
    
  
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let acell = "cella"
        var cell = DYtableview.cellForRow(at: indexPath) as? DYTableViewCell
     
        if cell == nil{
            cell = (DYTableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: acell) as? DYTableViewCell)!
        }
        cell?.tiao = self
//         let url = URL(string:headimage[indexPath.row] )
//        cell?.headimage.setImageWith(url!, placeholderImage: UIImage(named: "head"))
//        cell?.namelable.text = screenname[indexPath.row]
        cell?.contentLable.text = contenString[indexPath.row]
        cell?.timelable.text = time[indexPath.row]

        // 通过Lable的文字获取自适应区域
//        var rect:CGRect = (cell?.contentLable!.text!.boundingRect(with: CGSize(width: 260.0 ,height: 10000.0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:cell?.contentLable!.font], context: nil))!
//        //根据刚才的所获取到自适应宽高加上适当值来设置lable的宽高
////        var width:CGFloat = rect.width + 48
//        var lableheight:CGFloat = rect.height + 2
////        设置内容的行高
////
//        cell?.contentLable.frame = CGRect(x: 10, y: 63, width: 327, height: getLabHeigh(labelStr: (cell?.contentLable.text)!, font: (cell?.contentLable.font)!, width: 327, lineSpacing: 0))

        
        
        cell?.frame = DYtableview.bounds
        cell?.reloadData(imagea:contenimage[indexPath.row] )
    
        return cell!
        
        
    }

    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    
    //cell的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print(123)
    }
    
    //下拉刷新方法
    @objc func headerRefresh(){
        self.loc.update()
        contenimage.removeAll()
        headimage.removeAll()
        contenString.removeAll()
        screenname.removeAll()
        time.removeAll()
         DYtableview.reloadData()//重新加载数据
        DYtableview.mj_header.endRefreshing()
    }
    //上拉刷新方法
    @objc func footerRefresh(){
//
//        self.loc.update()
//        contenimage.removeAll()
//        headimage.removeAll()
//        contenString.removeAll()
//        screenname.removeAll()
//        time.removeAll()
//        DYtableview.reloadData()//重新加载数据
        //延迟5秒执行
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.DYtableview.mj_footer.endRefreshing()
            self.footer.setTitle("暂无更多数据", for: .idle)
        }
   
    }
}
